package com.sq.partner.manager.service.impl;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.dao.RuleInfoDao;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.RuleInfoService;
import com.sq.partner.manager.util.DocToHtml;
import com.sq.partner.manager.util.Page;

@Service
public class RuleInfoServiceImpl implements RuleInfoService {

	@Resource
	RuleInfoDao ruleInfoDao;
	
	@Override
	public List<Map<String , String >> queryNlifePlans(String deptCode) throws SQLException{
		return ruleInfoDao.queryNlifePlans( deptCode);
	}
	
	@Override
	public void deletOldInfoByPlanNo(String planNo) throws Exception{
		ruleInfoDao.deletOldInfoByPlanNo(planNo);
	}

	@Override
	public void insertNewPlanInfo(Map<String, String> map) throws Exception {
		ruleInfoDao.insertNewPlanInfo(map);
	}

	@Override
	public Map<String, String> queryPlanInfosByPlanNo(String planNo)
			throws Exception {
		return ruleInfoDao.queryPlanInfosByPlanNo(planNo);
	}

	@Override
	public List<InputStream> getFileInputStream(MultipartFile[] myfiles) throws Exception {
		List<InputStream> inputStreams=new ArrayList<InputStream>();
		try {
			for(MultipartFile file : myfiles){
				//获取文件名
				String fileNameStr = file.getOriginalFilename();
				int lastDotIndex = fileNameStr.lastIndexOf(".");
				//获取文件后缀名
				String extName = fileNameStr.substring(lastDotIndex+1,fileNameStr.length());
				if(-1==lastDotIndex){
					throw new AppException("请选择扩展名为.doc 或 .docx 的文件上传");
				}else{
					
					if(!"doc".equals(extName.toLowerCase()) && !"docx".equals(extName.toLowerCase())){
						throw new AppException("请选择扩展名为.doc 或 .docx 的文件上传");
					}
				}
				if (file.isEmpty()) {
					throw new AppException("上传失败，上传文件不能为空！");
				} else {
					//获取文件全名
					String fileName =  file.getOriginalFilename();
					InputStream in = file.getInputStream();
					
					//判断走哪个逻辑 
					if("doc".equals(extName.toLowerCase())){
						DocToHtml.dox(file.getOriginalFilename().split("\\.")[0], file.getOriginalFilename().split("\\.")[1], "index");
					}else if("docx".equals(extName.toLowerCase())){
						String content = DocToHtml.docx(in, "index");
						
						//存数据库
						Map<String,String> map = new HashMap<String, String>();
						map.put("riskCode", file.getOriginalFilename().split("\\.")[0]);
						map.put("riskContent", content);
						ruleInfoDao.insertNewRiskContent(map);
					}
					InputStream inputStream = file.getInputStream();
					inputStreams.add(inputStream);
				}
			}
		} catch (IOException e) {
			throw new AppException("上传错误!请稍后重试");
		}
		return inputStreams;
	}

	@Override
	public List<Map<String, String>> loadRiskContent(Map<String, Object> map)
			throws Exception {
		List<Map<String,String>> rsList = ruleInfoDao.loadRiskContentList(map); 
		return rsList;
	}

	@Override
	public int loadRiskContentCount(Map<String, Object> map) throws Exception {
		return ruleInfoDao.loadRiskContentListCount(map);
	}

	@Override
	public boolean isRiskFileExist(String riskCode) throws Exception{
		//判断条款文件是否已存在
		return ruleInfoDao.isRiskFileExist(riskCode);
	}

	@Override
	public String previewRiskContent(String riskCode) throws Exception {
		return ruleInfoDao.previewRiskContent(riskCode);
	}
	
	@Override
	public Page<Map<String, Object>> queryPlanPageByCriteria(Page<Map<String, Object>> page) throws Exception{
		return ruleInfoDao.queryPlanPageByCriteria(page);
	}
	
	
}
